function spectrum = spectrum1D(varargin)
% REQUIREMENTS:
% - x and h should be in the same unit
%
% FIELDS:
% - x
% - x_unit
% - h
% - h_unit
%
% EXAMPLES:
%  surface = surface1D(x,h);
%  surface = surface1D(surface1D);

switch nargin
    case 1
        surface = varargin{1};
        assert(isa(surface,'surface1D'),'The single input must be of class: surface1D');
        x = surface.x;
        dx = surface.dx;
        h = surface.h;
        mode_id = 0:(floor(length(x)/2));
        mode_value = zeros(2,length(mode_id));
        sampling_freq = 1/dx;
        for j = 1:length(mode_id)
            mode_value(1,j) = dx*h*eigenf_phi1(j-1,x);
            mode_value(2,j) = dx*h*eigenf_phi2(j-1,x);
        end
        spectrum.mode_id = mode_id;
        spectrum.mode_value = mode_value;
        spectrum.sampling_freq = sampling_freq;
        spectrum = class(spectrum,'spectrum1D');
    case 2

    otherwise
        fprintf(1,'Unfinished\n');
end
end

function phi1 = eigenf_phi1(n,x)
    dx = x(2)-x(1);
    x_max = max(x)+dx;
    k = sqrt(2/x_max);
    phi1 = k*sin(2*n*pi*x/x_max);
    if(size(phi1,1)==1)% Return a column vector
        phi1 = phi1';
    end
end

function phi2 = eigenf_phi2(n,x)
    dx = x(2)-x(1);   
    x_max = max(x)+dx;
    if n==0
        phi2 = sqrt(1/x_max)*ones(size(x));
    else
        k = sqrt(2/x_max);
        phi2 = k*cos(2*n*pi*x/x_max);
    end
    if(size(phi2,1)==1)% Return a column vector
        phi2 = phi2';
    end    
end